const MyPromise = require("../template.js")

/** 十三、Promise.allSettled 测试 */

// 测试 1: 所有 Promise 都成功
function testAllSettledAllFulfilled() {
  const p1 = MyPromise.resolve(1)
  const p2 = MyPromise.resolve(2)
  const p3 = MyPromise.resolve(3)

  MyPromise.allSettled([p1, p2, p3]).then((results) => {
    console.log(
      "13.1 All fulfilled - 结果数量:",
      results.length === 3
    )
    console.log(
      "13.2 All fulfilled - 第一个状态:",
      results[0].status === "fulfilled" && results[0].value === 1
    )
    console.log(
      "13.3 All fulfilled - 第二个状态:",
      results[1].status === "fulfilled" && results[1].value === 2
    )
    console.log(
      "13.4 All fulfilled - 第三个状态:",
      results[2].status === "fulfilled" && results[2].value === 3
    )
  })
}

// 测试 2: 所有 Promise 都失败
function testAllSettledAllRejected() {
  const p1 = MyPromise.reject("Error 1")
  const p2 = MyPromise.reject("Error 2")
  const p3 = MyPromise.reject("Error 3")

  MyPromise.allSettled([p1, p2, p3]).then((results) => {
    console.log(
      "13.5 All rejected - 第一个状态:",
      results[0].status === "rejected" && results[0].reason === "Error 1"
    )
    console.log(
      "13.6 All rejected - 第二个状态:",
      results[1].status === "rejected" && results[1].reason === "Error 2"
    )
    console.log(
      "13.7 All rejected - 第三个状态:",
      results[2].status === "rejected" && results[2].reason === "Error 3"
    )
  })
}

// 测试 3: 混合成功和失败
function testAllSettledMixed() {
  const p1 = MyPromise.resolve(1)
  const p2 = MyPromise.reject("Fail")
  const p3 = MyPromise.resolve(3)

  MyPromise.allSettled([p1, p2, p3]).then((results) => {
    console.log(
      "13.8 Mixed - 成功的结果:",
      results[0].status === "fulfilled" && results[0].value === 1
    )
    console.log(
      "13.9 Mixed - 失败的结果:",
      results[1].status === "rejected" && results[1].reason === "Fail"
    )
    console.log(
      "13.10 Mixed - 成功的结果:",
      results[2].status === "fulfilled" && results[2].value === 3
    )
  })
}

// 测试 4: 包含普通值
function testAllSettledWithNonPromise() {
  const p1 = MyPromise.resolve(1)
  const p2 = 2 // 普通值
  const p3 = MyPromise.reject("Fail")

  MyPromise.allSettled([p1, p2, p3]).then((results) => {
    console.log(
      "13.11 Non-promise - Promise 值:",
      results[0].status === "fulfilled" && results[0].value === 1
    )
    console.log(
      "13.12 Non-promise - 普通值:",
      results[1].status === "fulfilled" && results[1].value === 2
    )
    console.log(
      "13.13 Non-promise - 失败值:",
      results[2].status === "rejected" && results[2].reason === "Fail"
    )
  })
}

// 测试 5: 空数组
function testAllSettledEmpty() {
  MyPromise.allSettled([]).then((results) => {
    console.log("13.14 Empty array:", results.length === 0)
  })
}

// 测试 6: 异步 Promise
function testAllSettledAsync() {
  const p1 = new MyPromise((resolve) => setTimeout(() => resolve(1), 100))
  const p2 = new MyPromise((_, reject) => setTimeout(() => reject("Slow Fail"), 50))
  const p3 = new MyPromise((resolve) => setTimeout(() => resolve(3), 30))

  MyPromise.allSettled([p1, p2, p3]).then((results) => {
    console.log(
      "13.15 Async - 保持顺序:",
      results[0].status === "fulfilled" && results[0].value === 1
    )
    console.log(
      "13.16 Async - 中间失败:",
      results[1].status === "rejected" && results[1].reason === "Slow Fail"
    )
    console.log(
      "13.17 Async - 最快完成:",
      results[2].status === "fulfilled" && results[2].value === 3
    )
  })
}

testAllSettledAllFulfilled()
testAllSettledAllRejected()
testAllSettledMixed()
testAllSettledWithNonPromise()
testAllSettledEmpty()
testAllSettledAsync()

